﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Adex;
using MediaMonitor.Common;
using Telerik.Web.UI;

namespace MediaMonitor.Admin{
public partial class Data_Mgmt_Outdoor : System.Web.UI.Page
{
    DateTime sDate = DateTime.MinValue, eDate = DateTime.MinValue;
    protected void Page_Load(object sender, EventArgs e)
    {
        Utility.AddDateControlToPage(this.Page);
        SetCalendarDates();
    }


    private void SetCalendarDates()
    {
        DataTable dt = OutdoorLog.Query().DISTINCT().SetSelectList("RFDate").ExecuteDataSet().Tables[0];
        foreach (DataRow dr in dt.Rows)
        {

            RadCalendarDay d1 = new RadCalendarDay(calS);
            d1.Date = Utility.ToDateTime(dr["RFDate"]);
            d1.IsDisabled = false;
            d1.IsSelectable = true;

            d1.ItemStyle.BackColor = System.Drawing.Color.PowderBlue;
            d1.ItemStyle.ForeColor = System.Drawing.Color.White;

            calS.SpecialDays.Add(d1);

            RadCalendarDay d2 = new RadCalendarDay(calE);
            d2.Date = Utility.ToDateTime(dr["RFDate"]);
            d2.IsDisabled = false;
            d2.IsSelectable = true;

            d2.ItemStyle.BackColor = System.Drawing.Color.PowderBlue;
            d2.ItemStyle.ForeColor = System.Drawing.Color.White;
            calE.SpecialDays.Add(d2);
        }



    }

    protected void radGrid_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
    {
        //DataTable dt = Adex.VCommercialLog.Query().ORDER_BY("RFDate DESC, TimeStart").ExecuteDataSet().Tables[0];
        //DataTable dt = Adex.OutdoorLog.Query().SetSelectList("RFDate").AND("CountryID", AppUser.GetCountry()).DISTINCT().ExecuteDataSet().Tables[0];
        DataTable dt = Adex.OutdoorLog.Query().SetSelectList("RFDate").AND("CountryID", AppUser.GetCountry()).DISTINCT().ExecuteDataSet().Tables[0];
        
        radGrid.DataSource = dt;
    }
    protected void radGrid_DeleteCommand(Object source, Telerik.Web.UI.GridCommandEventArgs e)
    {

        //check deletion of child rows
        if (e.Item.OwnerTableView.DataSourceID == "sqlDSOut")
        {
            string detailPkId = e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["OutdoorLogID"].ToString();
            try
            {
               OutdoorLog.Delete(detailPkId);
            }
            catch
            {
                AjaxFuncs.MsgBox(this, "Failed to delete this record, because related data (child records) exists in other tables");
            }

            return;
        }



        DateTime pkId = Utility.ToDateTime(radGrid.MasterTableView.DataKeyValues[e.Item.ItemIndex]["RFDate"].ToString());

        try
        {
            RfController.DeleteAllOutDoorByDate(pkId, AppUser.GetCountry());

        }
        catch
        {
            AjaxFuncs.MsgBox(this, "Failed to delete this record, because related data (child records) exists in other tables");
        }

        radGrid.Rebind();
    }


    protected void btnDeleteAll_Clicked(object sender, EventArgs e)
    {
        bool tmp1 = DateTime.TryParse(calS.SelectedDate.ToShortDateString(), out sDate);
        bool tmp2 = DateTime.TryParse(calE.SelectedDate.ToShortDateString(), out eDate);
        if (sDate < Utility.ToDateTime("01/01/2000"))
            sDate = Utility.ToDateTime("01/01/2000");

        if (eDate < Utility.ToDateTime("01/01/2000"))
            eDate = Utility.ToDateTime("01/01/2000");

        if ((sDate.ToString() != "01/01/2000 12:00:00 AM") && (eDate.ToString() != "01/01/2000 12:00:00 AM"))
        {
            // 7 for Outdoor//0 means delete on condition
            RfController.DeleteAllMediaByDateBetween(AppUser.GetCountry(), Utility.ToDateTime(sDate as object), Utility.ToDateTime(eDate as object), 7, 0);
            SetCalendarDates();
            radGrid.Rebind();
            string script = "alert(\"Records are deleted successfully.\");";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "ServerControlScript", script, true);
        }
        else
        {
            string script = "alert(\"Please select dates first!\");";
            ScriptManager.RegisterStartupScript(this, this.GetType(), "ServerControlScript", script, true);

        }
    }

    protected void btnDeleteEverything_Clicked(object sender, EventArgs e)
    {

        bool tmp1 = DateTime.TryParse(calS.SelectedDate.ToShortDateString(), out sDate);
        bool tmp2 = DateTime.TryParse(calE.SelectedDate.ToShortDateString(), out eDate);
        if (sDate < Utility.ToDateTime("01/01/2000"))
            sDate = Utility.ToDateTime("01/01/2000");

        if (eDate < Utility.ToDateTime("01/01/2000"))
            eDate = Utility.ToDateTime("01/01/2000");

        // 7 for Outdoor//1 means delete ALL
        RfController.DeleteAllMediaByDateBetween(AppUser.GetCountry(), Utility.ToDateTime(sDate as object), Utility.ToDateTime(eDate as object), 7, 1);
        SetCalendarDates();
        radGrid.Rebind();
        string script = "alert(\"Records are deleted successfully.\");";
        ScriptManager.RegisterStartupScript(this, this.GetType(), "ServerControlScript", script, true);
    }


}
}
